package cn.iocoder.yudao.module.distm.service.quarterplan;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.*;
import cn.iocoder.yudao.module.distm.controller.admin.quarterplan.vo.*;
import cn.iocoder.yudao.module.distm.dal.dataobject.quarterplan.QuarterPlanDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.distm.dal.mysql.quarterplan.QuarterPlanMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.distm.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY;

/**
 * 配送管理-季度计划 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class QuarterPlanServiceImpl implements QuarterPlanService {

    @Resource
    private QuarterPlanMapper quarterPlanMapper;

    @Override
    public Long createQuarterPlan(QuarterPlanSaveReqVO createReqVO) {
        // 插入
        QuarterPlanDO quarterPlan = BeanUtils.toBean(createReqVO, QuarterPlanDO.class);
        quarterPlanMapper.insert(quarterPlan);
        // 返回
        return quarterPlan.getId();
    }

    @Override
    public void updateQuarterPlan(QuarterPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateQuarterPlanExists(updateReqVO.getId());
        // 更新
        QuarterPlanDO updateObj = BeanUtils.toBean(updateReqVO, QuarterPlanDO.class);
        quarterPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteQuarterPlan(Long id) {
        // 校验存在
        validateQuarterPlanExists(id);
        // 删除
        quarterPlanMapper.deleteById(id);
    }

    @Override
        public void deleteQuarterPlanListByIds(List<Long> ids) {
        // 校验存在
        validateQuarterPlanExists(ids);
        // 删除
        quarterPlanMapper.deleteByIds(ids);
        }

    private void validateQuarterPlanExists(List<Long> ids) {
        List<QuarterPlanDO> list = quarterPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(QUARTER_PLAN_NOT_EXISTS);
        }
    }

    private void validateQuarterPlanExists(Long id) {
        if (quarterPlanMapper.selectById(id) == null) {
            throw exception(QUARTER_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public QuarterPlanDO getQuarterPlan(Long id) {
        return quarterPlanMapper.selectById(id);
    }

    @Override
    public PageResult<QuarterPlanDO> getQuarterPlanPage(QuarterPlanPageReqVO pageReqVO) {
        return quarterPlanMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public QuarterPlanImportRespVO importQuarterPlanList(List<QuarterPlanImportExcelVO> importPlans, boolean isUpdateSupport) {
        // 1.1 参数校验
        if (CollUtil.isEmpty(importPlans)) {
            throw exception(QUARTER_PLAN_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 遍历，逐个创建 or 更新
        QuarterPlanImportRespVO respVO = QuarterPlanImportRespVO.builder().createPlans("").build();

        List<QuarterPlanDO>  quarterPlanDOS = new ArrayList<>(importPlans.size());
        importPlans.forEach(importPlan -> {
            QuarterPlanDO insertPlan = BeanUtils.toBean(importPlan, QuarterPlanDO.class);
            quarterPlanDOS.add(insertPlan);
        });
        quarterPlanMapper.insertBatch(quarterPlanDOS);

        respVO.setCreatePlans(String.valueOf(importPlans.size()));
        return respVO;
    }

}